﻿<template>
  <a-modal
    title="编辑出入库记录"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="任务号" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入任务号" v-decorator="['taskno']" />
        </a-form-item>
        <a-form-item label="任务方式" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select style="width: 100%" placeholder="请选择任务方式" v-decorator="['taskmodel', {rules: [{ required: true, message: '请选择任务方式！' }]}]">
            <a-select-option v-for="(item,index) in taskmodelData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="任务类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select style="width: 100%" placeholder="请选择任务类型" v-decorator="['tasktype', {rules: [{ required: true, message: '请选择任务类型！' }]}]">
            <a-select-option v-for="(item,index) in tasktypeData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="任务等级" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input-number placeholder="请输入任务等级" style="width: 100%" v-decorator="['tasklevel']" />
        </a-form-item>
        <a-form-item label="任务状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select style="width: 100%" placeholder="请选择任务状态" v-decorator="['taskstatus', {rules: [{ required: true, message: '请选择任务状态！' }]}]">
            <a-select-option v-for="(item,index) in taskstatusData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="来源" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入来源" v-decorator="['sourceplace']" />
        </a-form-item>
        <a-form-item label="目标" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入目标" v-decorator="['toplace']" />
        </a-form-item>
        <a-form-item label="托盘编码" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入托盘编码" v-decorator="['containercode']" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import {
    View_AccessDetailsEdit
  } from '@/api/modular/main/View_AccessDetailsManage'
  export default {
    data () {
      return {
	    Id: 0,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        record: {},
        taskmodelData: [],
        tasktypeData: [],
        taskstatusData: [],
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      // 初始化方法
      edit (record) {
        this.visible = true;
		this.Id = record.id;
        this.$nextTick(() => {
        });
        //深度拷贝 移除VUE的监听，防止INDEX页面值变动
        this.record = JSON.parse(JSON.stringify(record))
        const taskmodelOption = this.$options
        this.taskmodelData = taskmodelOption.filters['dictData']('task_model')
        const tasktypeOption = this.$options
        this.tasktypeData = tasktypeOption.filters['dictData']('task_type')
        const taskstatusOption = this.$options
        this.taskstatusData = taskstatusOption.filters['dictData']('task_status')
        this.$nextTick(() => {
          this.form.setFieldsValue(
            {
              taskno: record.taskno,
              taskmodel: record.taskmodel,
              tasktype: record.tasktype,
              tasklevel: record.tasklevel,
              taskstatus: record.taskstatus,
              sourceplace: record.sourceplace,
              toplace: record.toplace,
              containercode: record.containercode
            }
          )
        })
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (values[key] == null) continue
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
                 this.record[key] = values[key]
              } else {
                 this.record[key] = values[key]
              }
            }
            View_AccessDetailsEdit(this.record).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', this.record)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败：' + JSON.stringify(res.message))
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          }else{
            this.confirmLoading = false
          } 
        });
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
